package com.example.study.jetpack.compose

/**
 *  created by 20300
 *   on 2022/3/15
 *   describe:https://blog.csdn.net/Mr_Tony/article/details/118971169?spm=1001.2014.3001.5502
 */
class Compose的Navigation {
    /*依赖:implementation "androidx.navigation:navigation-compose:2.4.0-alpha04"

    NavHostController
    在Compose中导航主要是用NavHostController来进行控制.通常这个实例使用rememberNavController()来获取
    val navController = rememberNavController()

    NavBackStackEntry
    NavBackStackEntry存储了导航中回退栈的信息.可以通过以下方式获取
    val backstackEntry = navController.currentBackStackEntryAsState()
    val route = backstackEntry.value?.destination?.route //获取当前的路由状态

    路由跳转
    Navigation有多种方式进行跳转,这里记录下路由跳转(使用路由跳转可以做到组件之间的解耦).
    路由路径一般为字符串,需要使用composable(route)进行包括.示例:
    NavHost(
        navController = navController,//路由控制器
        startDestination = RallyScreen.Overview.name//导航开始页面
			){
        composable("route") {
            BillsBody(bills = UserData.bills) //组件内容,一个页面
        }
  }
  跳转则使用以下方式
  navController.navigate("route")

    路由传参数
    路由传参数的话一般跟在路径后面,接受参数由navArgument()来获取,接受参数示例如下
    composable(
    route = "$accountsName/{name}",
    arguments = listOf(
        navArgument("name") {
            // Make argument type safe
            type = NavType.StringType
        }
    ),
    deepLinks =  listOf(navDeepLink {
        uriPattern = "rally://$accountsName/{name}"
    })
) {
        entry -> // Look up "name" in NavBackStackEntry's arguments
    val accountName = entry.arguments?.getString("name")
    // Find first name match in UserData
    val account = UserData.getAccount(accountName)
    // Pass account to SingleAccountBody
    SingleAccountBody(account = account)
}
发送参数
navController.navigate("${RallyScreen.Accounts.name}/$accountName")

    深度链接:
    Navigation支持深度链接,由外部跳转进来
    首先,将深层链接添加到AndroidManifest.cml,需要为RallyActivity操作VIEW和类别BROWSABLE以及
    DEFAULT
    然后，使用data标记，添加scheme，host和pathPreFix
    此代码实验室将rally://accounts/{name}用作深层链接URl
    不需要在AndroidManifest中声明"name"参数.它将被Navigation解析为参数.
    AndroidManifest配置如下:
    <activity
    android:name=".RallyActivity"
    android:windowSoftInputMode="adjustResize"
    android:label="@string/app_name"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="rally" android:host="accounts" />
    </intent-filter>
</activity>

接受参数如下:
val accountsName = RallyScreen.Accounts.name

composable(
    "$accountsName/{name}",
    arguments = listOf(
        navArgument("name") {
            type = NavType.StringType
        },
    ),
    deepLinks =  listOf(navDeepLink {
        uriPattern = "rally://$accountsName/{name}"
    })
)

    */

}